package com.lj.ucenter.controller;

import com.lj.ucenter.properties.WxProperties;
import com.lj.utils.utils.Parser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author luojing
 * @version 1.0
 * @date 2021/7/5 13:47
 */
@CrossOrigin
//@RestController
@Controller
@RequestMapping("/ucenter/wx")
public class WxApiController {

    @Autowired
    private WxProperties wxProperties;

    //生成微信扫码的二维码
    @GetMapping("login")
    @ApiOperation("微信登录")
    public String getWxCode() throws UnsupportedEncodingException {
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect?appid={appid}&redirect_uri={redirectUri}&" +
                "response_type=code&scope=snsapi_login&state={state}#wechat_redirect";
        Map<String,String> map = new HashMap<>();
        map.put("appid",wxProperties.getAppid());
        map.put("redirectUri", URLEncoder.encode(wxProperties.getRedirecturl(),"utf-8"));
        map.put("state","atguigu");
        return "redirect:"+Parser.parse(baseUrl, map);
    }

}
